import type { AxiosRequestHeaders, AxiosResponse, InternalAxiosRequestConfig } from "axios"

export type Method = 'get' | 'post' | 'put' | 'patch' | 'delete'
export type ResponseType = 'json' | 'arraybuffer' | 'blob' | 'document' | 'text' | 'stream'
export type Recordable<T = any> = Record<string, T>

export type HttpConfigType = {
  TokenPreKey?: string,
  TokenKey?: string,
  TokenKeyPre?: string,
  Headers?: AxiosRequestHeaders,
  BaseURL?: string,
  Timeout?: number,
  Method?: Method
  // 需要跳转到登录页面的错误码
  unAuthCode?: (number | string)[]
  // 请求正确 后台返回的code码
  okCode?: string | number
  // 全局响应体处理
  dealResponse?: (res: AxiosResponse) => AxiosResponse
  // 全局请求体处理
  dealRequest?: (req: InternalAxiosRequestConfig) => InternalAxiosRequestConfig
  // 全局响应体错误处理
  dealResponseError?: (err) => any
  // 下载 responseType为'blob' 'arraybuffer' 默认返回全部response响应体
  ReturnResData?: boolean

}
export type ApiType = {
  // api接口
  api: string
  // 请求类型
  type?: 'get' | 'post' | 'put' | 'delete',
  // 请求头信息
  header?: any
  // 默认的基础数据
  model?: Recordable
  // 请求之前
  beforeFetch?: ( params: Recordable ) => any
  // 请求之后
  afterFetch?: ( data: Recordable)  => any
}

export interface ApiObject {
  url: string,
  method?: Method,
  baseURL?: string,
  headers?: AxiosRequestHeaders,
  params?: object | URLSearchParams,
  transformRequest?: Function,
  transformResponse?: Function,
  data?: object | ArrayBuffer | ArrayBufferView | URLSearchParams | FormData | File | Blob | ReadableStream,
  responseType?: ResponseType,
  responseEncoding?: 'utf8',
  // 请求之前
  beforeFetch?: ( params: Recordable ) => any
  // 请求之后
  afterFetch?: ( data: Recordable)  => any
  // 默认的基础数据
  model?: Recordable
  exparams?: ExConfigParams
}

export const httpDefaultConfig = {
  tokenPreKey : 'Authorization',
  defaultMethod: 'post',
  formDataContentType: 'application/x-www-form-urlencoded',
  returnResData: true,
}

export type ExConfigParams = {
  isFormData?: boolean,
  afterReponse?: (result: unknown) => unknown
  // 是否只返回data
  onlyData?: boolean
  // 请求正确的code码
  okCode?: number | string,
  // 重复请求
  ignoreRepeatRequest?: boolean
}

// 返回的配置项
export type JsonResult<T = any> = {
  code?: string | number;
  status?: boolean;
  message?: string;
  data?: T;
  [key: string]: any;
}
